﻿using Common.Contract;
using Common.Contract.Authentication.Record;
using Common.Contract.ProductHandbook.Record;
using Common.Contract.ProductHandbook.Request;
using Common.Service.Command;
using CrossSelling.Model.Interface;
using CrossSelling.Model.Model;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace CrossSelling.Core.ServiceCommand.Admin
{
    public class UpdateChecklistRuleServiceCommand : ProviderServiceCommand<IProviderFactory, UpdateChecklistRuleRequest, ResponseRecord>
    {
        private UserRecord Dao;
        private string dataUpdate;
        private int productId;
        public UpdateChecklistRuleServiceCommand(IProviderFactory providers) : base(providers) { }
        protected override void Validate(UpdateChecklistRuleRequest request)
        {
            if (request == null) throw new ArgumentNullException("request");
            if (!request.Header.ApplicationKey.HasValue || request.Header.ApplicationKey == Guid.Empty)
            {
                throw new ArgumentException("ApplicationKey must be set", "request");
            }
            if (string.IsNullOrEmpty(request.DataUpdate))
            {
                throw new ArgumentException("Record must be set", "request");
            }
            else
            {
                dataUpdate = request.DataUpdate;
            }
            if (string.IsNullOrEmpty(request.ProductId) && int.Parse(request.ProductId) == 0)
            {
                throw new ArgumentException("Product must be set", "request");
            }
            else
            {
                productId = int.Parse(request.ProductId);
            }
            if (request.DaoChange == null)
            {
                throw new ArgumentException("MSBO Dao must be set", "request");
            }
            else
            {
                Dao = Request.DaoChange;
            }
        }

        protected override bool OnExecute(ResponseRecord response)
        {
            try
            {
                bool isUpdate = true;
                var array = JArray.Parse(Request.DataUpdate);
                List<ChecklistQuestionSubProductResult> listDocDocument = new List<ChecklistQuestionSubProductResult>();
                var firstDoc = JArray.Parse(array[1].ToString().Trim('{', '}'));
                string firstDoc1 = firstDoc[0].ToString();
                int firstDocId = int.Parse(Regex.Replace(firstDoc1, "[^0-9]+", string.Empty));
                int ruleId = 0;
                if (int.Parse(Request.RuleId) != 0 && Request.SubProductId != 0)
                {
                    ruleId = int.Parse(Request.RuleId);
                }
                else
                {
                    ruleId = (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
                }

                for (int i = 0; i < array.Count; i++)
                {
                    firstDoc = JArray.Parse(array[i].ToString().Trim('{', '}'));
                    firstDoc1 = firstDoc[0].ToString();
                    firstDocId = int.Parse(Regex.Replace(firstDoc1, "[^0-9]+", string.Empty));
                    if (!string.IsNullOrEmpty(firstDoc[1].ToString()))
                    {
                        ChecklistQuestionSubProductResult docCustomer = new ChecklistQuestionSubProductResult
                        {
                            AnswerGroup = ruleId,
                            QuestionId = firstDocId,
                            Answer = firstDoc[1].ToString(),
                            ProductId = productId,
                            QSpResultId = (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds + firstDocId,
                            SubProductId = Request.SubProductId
                        };
                        listDocDocument.Add(docCustomer);
                    }
                }
                isUpdate = Providers.ProductHandbooks.UpdateRuleChecklist(listDocDocument, ruleId, productId, Request.SubProductId);
                return isUpdate;
            }
            catch (Exception e)
            {
                throw new ArgumentException(e.Message);
            }
        }
    }
}
